package com.ph.irp.base.verifyproject.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectMaterialService;
import com.ph.irp.dto.MaterialDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.model.PBaseVerifyprojectMaterial;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class VerifyprojectMaterialAction extends ActionSupport implements
		ShowPageList {
	private PBaseVerifyprojectMaterialService materialService;
	private PageDTO page;
	private String cpage;
	private String rowIndex;
	private MaterialDTO mdto;
	private List<MaterialDTO> mdtos;
	private List<MaterialDTO> tempmdtos;
	private PBaseVerifyprojectMaterial verifyprojectMaterial;
	private Object[] rowlist = null;

	public String getRowIndex() {
		return rowIndex;
	}

	public void setRowIndex(String rowIndex) {
		this.rowIndex = rowIndex;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public void setMaterialService(
			PBaseVerifyprojectMaterialService materialService) {
		this.materialService = materialService;
	}

	public MaterialDTO getMdto() {
		return mdto;
	}

	public void setMdto(MaterialDTO mdto) {
		this.mdto = mdto;
	}

	public List<MaterialDTO> getMdtos() {
		return mdtos;
	}

	public void setMdtos(List<MaterialDTO> mdtos) {
		this.mdtos = mdtos;
	}

	public PBaseVerifyprojectMaterial getVerifyprojectMaterial() {
		return verifyprojectMaterial;
	}

	public void setVerifyprojectMaterial(
			PBaseVerifyprojectMaterial verifyprojectMaterial) {
		this.verifyprojectMaterial = verifyprojectMaterial;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showMaterialList() throws Throwable {
		// verifyprojectMaterial = null;
		tempmdtos = materialService
				.getMaterialList(new PBaseVerifyprojectMaterial());
		page = PageUtil.generatePageDTO(tempmdtos,
				"showMaterialPageList.action", cpage);
		mdtos = page.getResList();
		verifyprojectMaterial = null;
		mdto = null;
		return "success";
	}

	public String getMaterialByCondition() throws Throwable {
		String name = verifyprojectMaterial.getMaterialName().trim();
		String code = verifyprojectMaterial.getMaterialCode().trim();
		verifyprojectMaterial.setMaterialName(name);
		verifyprojectMaterial.setMaterialCode(code);
		tempmdtos = materialService.getMaterialList(verifyprojectMaterial);

		page = PageUtil.generatePageDTO(tempmdtos,
				"showMaterialPageList.action", cpage);
		mdtos = page.getResList();
		verifyprojectMaterial = null;
		return "success";
	}

	// List页面跳转到update修改页面
	public String showEditMaterial() throws Throwable {
		Integer materialId = Integer.valueOf((String) rowlist[0]);
		mdto = materialService.getMaterialById(materialId);
		return "success";
	}

	public String editMaterial() throws Throwable {
		this.materialService.updateMaterial(this.mdto.getMaterial());
		return "success";
	}

	public String showAddMaterial() throws Throwable {
		mdto = new MaterialDTO();
		return "success";
	}

	public String addMaterial() throws Throwable {
		this.materialService.addMaterial(this.mdto.getMaterial());
		return "success";
	}

	public String deleteMaterial() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			materialService
					.deleteMaterial(Integer.valueOf((String) rowlist[i]));
		}
		return "success";
	}

	public String selectMaterial() throws Throwable {
		if (verifyprojectMaterial != null) {
			String name = verifyprojectMaterial.getMaterialName().trim();
			String code = verifyprojectMaterial.getMaterialCode().trim();
			verifyprojectMaterial.setMaterialName(name);
			verifyprojectMaterial.setMaterialCode(code);
			tempmdtos = materialService.getMaterialList(verifyprojectMaterial);
			verifyprojectMaterial = null;
		} else {
			tempmdtos = materialService
					.getMaterialList(new PBaseVerifyprojectMaterial());

		}

		page = PageUtil.generatePageDTO(tempmdtos,
				"showMaterialPageList.action", cpage);
		mdtos = page.getResList();
		verifyprojectMaterial = null;
		return "success";
	}

	public String selectMultiMaterial() throws Throwable {
		rowIndex = ServletActionContext.getRequest().getParameter("rowIndex");
		tempmdtos = materialService.getMaterialList(verifyprojectMaterial);
		page = PageUtil.generatePageDTO(tempmdtos,
				"showMaterialPageList.action", cpage);
		mdtos = page.getResList();
		verifyprojectMaterial = null;
		return "success";
	}

	public String selectByMaterialAttribute() throws Throwable {
		if (verifyprojectMaterial != null) {
			verifyprojectMaterial.setMaterialAttribute("自制");
			tempmdtos = materialService.getMaterialList(verifyprojectMaterial);
			page = PageUtil.generatePageDTO(tempmdtos,
					"showMaterialPageList.action", cpage);
			mdtos = page.getResList();
			verifyprojectMaterial = null;
		} else {
			PBaseVerifyprojectMaterial material = new PBaseVerifyprojectMaterial();
			material.setMaterialAttribute("自制");
			tempmdtos = materialService.getMaterialList(material);
			page = PageUtil.generatePageDTO(tempmdtos,
					"showMaterialPageList.action", cpage);
			mdtos = page.getResList();
			material = null;
		}
		return "success";
	}

	public String showPageList() throws Throwable {
		if (tempmdtos != null) {
			page = PageUtil.generatePageDTO(tempmdtos,
					"showMaterialPageList.action", cpage);
			mdtos = page.getResList();
			if (rowIndex != null) {
				return "select";
			} else {
				return "success";
			}
		} else {
			return "input";
		}
	}

}
